package com.zt.rongda.sign.service;

import java.util.List;

import com.zt.rongda.constant.BusConstant;
import com.zt.rongda.sign.inter.SignMapper;
import com.zt.rongda.sign.model.Sign;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SignService extends BaseService {

    @Autowired
    private SignMapper signMapper;

    /**
     * 查询
     * 
     * @param reqBean
     * @return
     */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select a.*,e.EMP_NAME from SIGN a LEFT JOIN EMPLOYEE e ON e.EMP_ID = a.EMP_ID where 1=1");
        if (!BusConstant.SUPER_ADMIN_CODE.equals(reqBean.getUserId())) {
            sqlBuilder.addWhere("a.EMP_ID",
                    "(SELECT u.SOURCE_ID FROM SYS_USER u where u.USER_ID = '" + reqBean.getUserId() + "')");
        }
        sqlBuilder.addTimeWhere("a.SIGN_TIME",reqBean.getInfo("startTime"),">=");
        sqlBuilder.addTimeWhere("a.SIGN_TIME",reqBean.getInfo("endTime"),"<=");
        sqlBuilder.addWhereLike2("a.LOCATION",reqBean.getInfo("location"));
        sqlBuilder.orderBy("ORDER BY a.SIGN_TIME desc");
        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 通过id查询
     * 
     * @param signId
     * @return
     */
    public Sign getById(String signId) {
        return signMapper.selectByPrimaryKey(signId);
    }

    /**
     * 新增
     * 
     * @param sign
     */
    public void add(Sign sign) {
        sign.setSignId(stringId());// 手动设置Id

        sign.setMakeTime(new Date());
        sign.setSignTime(new Date());
        sign.setModifyTime(sign.getMakeTime());
        signMapper.insert(sign);
    }

    /**
     * 修改
     * 
     * @param sign
     */
    public void update(Sign sign) {
        String signId = sign.getSignId();
        if (StringUtil.isEmpty(signId)) {
            throw new BusinessException("获取主键失败");
        }

        sign.setModifyTime(new Date());
        signMapper.updateByPrimaryKeySelective(sign);
    }

    /**
     * 删除
     * 
     * @param signId
     */
    public void delete(String signId) {
        if (StringUtil.isEmpty(signId)) {
            throw new BusinessException("获取主键失败");
        }
        signMapper.deleteByPrimaryKey(signId);
    }

}